package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class AdapterActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {

    private Spinner sp_1;
    private Spinner sp_2;
    private Spinner sp_3;
    private Spinner sp_4;
    private TextView tv_1;
    private String[] data = {"香蕉", "橘子", "苹果", "菠萝", "哈密瓜"};
    private int[] imgs = {R.drawable.android1,R.drawable.android2,R.drawable.android3};
    private List<Map<String, Object>> data_3;
    private List<ItemData> data_4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapter);
        tv_1 = findViewById(R.id.tv_1);

        sp_1 = findViewById(R.id.sp_1);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.spinner_itemview, data);
        sp_1.setAdapter(arrayAdapter);
        sp_1.setSelection(0);
        sp_1.setOnItemSelectedListener(this);

        sp_2 = findViewById(R.id.sp_2);
        sp_2.setAdapter(arrayAdapter);
        sp_2.setSelection(0);
        sp_2.setOnItemSelectedListener(this);

        data_3 = new ArrayList<>();
        Arrays.stream(data).forEach(s -> {
            data_3.add(new HashMap<String, Object>() {{
                put("img", imgs[new Random().nextInt(10) % imgs.length]);
                put("name", s);
            }});
        });


        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data_3, R.layout.spinner_textimg, new String[]{"img", "name"}, new int[]{R.id.iv_img, R.id.tv_name});
        sp_3 = findViewById(R.id.sp_3);
        sp_3.setAdapter(simpleAdapter);
        sp_3.setSelection(0);
        sp_3.setOnItemSelectedListener(this);

        data_4 = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            for (int i1 = 0; i1 < 5; i1++) {
                data_4.add(new ItemData(imgs[new Random().nextInt(10) % imgs.length],i+"-"+i1+":::"+data[i] ,i+data[i]+i1+","));
            }
        }
        sp_4 = findViewById(R.id.sp_4);
        sp_4.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return data_4.size();
            }

            @Override
            public Object getItem(int position) {
                return data_4.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                // 获取布局 复用
                if(convertView == null) {
                    convertView = LayoutInflater.from(AdapterActivity.this).inflate(R.layout.spinner_mix, null);

                    ImageView iv = convertView.findViewById(R.id.iv_img);
                    TextView tv_name = convertView.findViewById(R.id.tv_name);
                    TextView tv_detail = convertView.findViewById(R.id.tv_detail);
                    holder = new ViewHolder(iv,tv_name,tv_detail);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                // 设置数据
                holder.getIv().setImageResource(data_4.get(position).getImg());
                holder.getTv_name().setText(data_4.get(position).getName());
                holder.getTv_detail().setText(data_4.get(position).getDesc());
                return convertView;

            }
        });
        sp_4.setSelection(0);
        sp_4.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "点我干啥", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String dataStr = null;
        String text = "";
        if (parent.getId() == R.id.sp_1) {
            text = "第一个";
            dataStr = data[position];
        } else if (parent.getId() == R.id.sp_2) {
            text = "第二个";
            dataStr = data[position];
        } else if (parent.getId() == R.id.sp_3) {
            text = "第三个";
            dataStr = (String) data_3.get(position).get("name");
        } else if (parent.getId() == R.id.sp_4) {
            text = "第四个";
            dataStr = data_4.get(position).getName();

        }
        Toast.makeText(this, String.format("%s选择的是:%s", text,dataStr ), Toast.LENGTH_SHORT).show();
        tv_1.setText(text + " 选择了：" + dataStr);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        tv_1.setText("啥也没选！");
    }
    class ItemData{
        private int img;
        private String name;
        private String desc;

        public ItemData(int img, String name, String desc) {
            this.img = img;
            this.name = name;
            while(desc.length() <50) {
                desc+=desc;
            }
            this.desc = desc;
        }

        public ItemData(int img) {

        }

        public int getImg() {
            return img;
        }

        public void setImg(int img) {
            this.img = img;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
    class ViewHolder{
        private ImageView iv;
        private TextView tv_name;
        private TextView tv_detail;

        public ViewHolder(ImageView iv, TextView tv_name, TextView tv_detail) {
            this.iv = iv;
            this.tv_name = tv_name;
            this.tv_detail = tv_detail;
        }


        public ImageView getIv() {
            return iv;
        }

        public void setIv(ImageView iv) {
            this.iv = iv;
        }

        public TextView getTv_name() {
            return tv_name;
        }

        public void setTv_name(TextView tv_name) {
            this.tv_name = tv_name;
        }

        public TextView getTv_detail() {
            return tv_detail;
        }

        public void setTv_detail(TextView tv_detail) {
            this.tv_detail = tv_detail;
        }
    }
}